package com.ruoyi.business.tools;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.ruoyi.business.domain.Employee;
import com.ruoyi.business.domain.vo.EmployeePdf;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;


public class pdfTool {
    /*
     * 创建pdf
     * */
    public Boolean creatPdf(EmployeePdf entity, Long filename) throws DocumentException, IOException {
        //模板名称
        String templateFileName = RuoYiConfig.gettemplatePath() + "员工资料.xls";
        String fileName = RuoYiConfig.getUploadPath() + "/" + filename + ".xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(entity, fillConfig, writeSheet);
        }
        //excel转pdf
        String PDFfileurl = RuoYiConfig.getDownloadPath() + filename + ".pdf";
        ExcelToPdf.excel2pdf(fileName, PDFfileurl, 0);
        addImgToPdf(PDFfileurl,entity.getImg());
        return true;
    }
    /*
     * 加照片
     * */
    public void addImgToPdf(String pdfpath,String imgurl) throws IOException, DocumentException {
        if (StringUtils.isEmpty(imgurl)){
            return;
        }
        // 模板文件路径  tempFile
         File pdfFile = new File(pdfpath);
        if (!pdfFile.exists()) {
            return;
        }
        // 目标文件路径
        File resultFile = new File(pdfpath);
        // 读取模板文件
        FileInputStream input = new FileInputStream(pdfFile);
        PdfReader reader = new PdfReader(input);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(resultFile));
        // 头像图片
        String BaseDir= FileUploadUtils.getDefaultBaseDir()+""+imgurl.replace("profile/","");
        //String BaseDir="F:\\home\\sushe\\uploadPath\\avatar\\00385_潘闽廷.jpg";
        File file = new File(BaseDir);
        if (file.exists()) {
            try {
                Image image = Image.getInstance(BaseDir);
                // 根据域的大小缩放图片
                //image.scaleToFit(90, 220);
                // 设置新的宽度和高度
                float newWidth = 120;  // 新的宽度，单位是点（1 inch = 72 points）
                float newHeight = 150; // 新的高度，单位是点（1 inch = 72 points）
                image.scaleAbsolute(newWidth, newHeight);
                // 添加图片
                image.setAbsolutePosition(820, 890);
                PdfContentByte under = stamper.getOverContent(1);
                under.addImage(image);

            }catch (DocumentException e){
                throw e;
            }
        }
        stamper.close();
        reader.close();

    }
}
